using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Firestarter.DB.Schema.Logic;

namespace Firestarter.DB.Schema.Test.Logic
{
    public class TestUtilsDataTable
    {
        public static bool ContainsRow(DataTable dataTable, string fieldName, string fieldValue)
        {
            return GetRow(dataTable, fieldName, fieldValue) != null;
        }

        public static DataRow GetRow(DataTable dataTable, string fieldName, string fieldValue)
        {
            return GetRow(dataTable, new KeyValuePair<string, string>(fieldName, fieldValue));
        }

        public static DataRow GetRow(DataTable dataTable, params KeyValuePair<string, string>[] fieldValues)
        {
            return GetRow(dataTable, delegate(DataRow row)
            {
                bool isMatch = true;
                foreach (KeyValuePair<string, string> pair in fieldValues)
                {
                    isMatch &= String.Compare(DBUtilities.ConvertDBToString(row[pair.Key]), pair.Value, true) == 0;
                }
                return isMatch;
            });
        }

        public static DataRow GetRow(DataTable dataTable, Predicate<DataRow> predicate)
        {
            foreach (DataRow row in dataTable.Rows)
            {
                if (predicate(row)) return row;
            }
            return null;
        }
    }
}
